package repeat.personal.base;

import org.springframework.beans.factory.annotation.Autowired;
import repeat.personal.page.PageRequest;
import repeat.personal.page.PageResult;
import repeat.personal.service.CurdService;

import java.util.List;
import java.util.Optional;

/**
 * 通用CURD实现
 * @author: Zhanggq
 * @date: 2019/4/2 16:40
 **/
public class BaseCurdServiceImpl<M extends BaseRepository<T,Long>,T> implements CurdService<T> {
    @Autowired
    protected M repository;

    /**
     * 保存
     * @param record
     */
    @Override
    public void save(T record) {
        repository.save(record);
    }

    /**
     * 删除
     * @param record
     */
    @Override
    public void delete(T record) {
        repository.delete(record);
    }

    /**
     * 批量删除
     * @param records
     */
    @Override
    public void delete(List<T> records) {
        if(records.isEmpty()){
            return ;
        }
        for(T t : records){
            delete(t);
        }
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @Override
    public Optional<T> findById(Long id) {
        return repository.findById(id);
    }

    /**
     * 分页
     * @param pageRequest 自定义，统一分页查询请求
     * @return
     */
    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return null;

    }
}
